﻿HmJS.$Import('core.fx');

/*
---
version: 1.0-2
description: Scroll a 'film' of 'slides' trought a 'window'
license: MIT-style
download: http://mootools.net/forge/p/scroller
source: http://github.com/babs/mootools-scroller/

authors: babs

requires:
- core/1.2.4: '*'
- more/1.2.4: 'Fx.Scroll'

provides: Scroller
...
*/

HmJS.register('util.effect.Scroller', function ($ns) {

	return new Class({

		Implements: Options,

		options: {
			duration: 500,
			selectBy: "child",
			autostart: false,
			autostart_dir: "forward",
			sleep: 1000,
			transition: "sine:in:out",
			leftm: 0,
			topm: 0
		},

		length: 0,

		initialize: function (windowElement, options) {
			this.setOptions(options);
			this.windowElement = windowElement;
			this.slideStrip = windowElement.getChildren()[0];
			this.cChildIdx = 0;
			this.fx = new Fx.Scroll(windowElement, { duration: this.options.duration, link: 'cancel', transition: this.options.transition });
			if (this.options.selectBy == "class") {
				this.childs = this.slideStrip.getElements(".slide");
			} else {
				this.childs = this.slideStrip.getChildren();
			}
			this.childCoords = Array();
			for (var i = 0, len = this.childs.length; i < len; i++) {
				this.childCoords.push({
					left: this.childs[i].getCoordinates(this.windowElement)['left'],
					top: this.childs[i].getCoordinates(this.windowElement)['top']
				});
			}
			this.length = this.childs.length;
			this.periodicalID = 0;
			this.startdir = false;
			if (this.options.autostart == true) {
				if (this.options.autostart_dir != "back") {
					this.periodicalID = this.next.periodical(this.options.sleep + this.options.duration, this);
				} else {
					this.startdir = true;
					this.periodicalID = this.prev.periodical(this.options.sleep + this.options.duration, this);
				}
			}
		},

		updateTransition: function (opts) {
			this.setOptions(opts);
			this.fx.options.duration = this.options.duration;
			this.fx.options.transition = this.options.transision;
			if (this.periodicalID) {
				this.restart();
			}
		},

		stop: function () {
			if (this.periodicalID) {
				clearInterval(this.periodicalID);
				this.periodicalID = 0;
			}
		},

		start: function (back) {
			if (this.periodicalID) {
				clearInterval(this.periodicalID);
			}
			if (back) {
				this.startdir = true;
				this.periodicalID = this.prev.periodical(this.options.sleep + this.options.duration, this);
			} else {
				this.startdir = false;
				this.periodicalID = this.next.periodical(this.options.sleep + this.options.duration, this);
			}
		},

		next: function (stop) {
			if (stop) { this.stop(); }
			this.cChildIdx++;
			if (this.cChildIdx >= this.childs.length) {
				this.cChildIdx = 0;
			}
			this.gotoIdx();
		},

		restart: function () {
			this.stop();
			this.start(this.startdir);
		},

		prev: function (stop) {
			if (stop) { this.stop(); }
			this.cChildIdx--;
			if (this.cChildIdx < 0) {
				this.cChildIdx = this.childs.length - 1;
			}
			this.gotoIdx();
		},

		gotoIdx: function () {
			var s = this.childs[this.cChildIdx];
			var bl = s.getStyle('border-left').toInt();
			if (isNaN(bl)) { bl = 0; }
			var bt = s.getStyle('border-top').toInt();
			if (isNaN(bt)) { bt = 0; }

			var ccrds = this.childCoords[this.cChildIdx];
			this.curTrans = this.fx.start(ccrds.left - bl - this.options.leftm, ccrds.top - bl - this.options.topm);
		}

	});

});